﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using webapp4U;
using webapp4U.BOL;

namespace webapp4U.UI.Admin
{
    public partial class Albums : System.Web.UI.Page
    {
        private int CateID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(1000);

            if (Request.QueryString["Action"] == "Edit")
            {
                CateID = Convert.ToInt32(Request.QueryString["CateID"]);
                btAlbumAdd.CommandArgument = "Edit";
                if (!IsPostBack)
                    Load_AlbumByCateID(CateID);
            }
        }


        protected void btDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridViewItem.Rows)
            {
                bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
                if (isChecked)
                {
                    int ID = Convert.ToInt32(((CheckBox)row.FindControl("chkSelect")).ToolTip.Trim());
                    webapp4U.BOL.PhotoCategory.Delete(ID);
                }
            }
            this.DataBind();
        }


        protected string FilePhoto_Upload()
        {
            string strFileName = "";
            // kiem tra xem user co chon hinh dai dien ko
            if (FilePhoto.FileName != null && FilePhoto.FileName != "")
            {
                // lay ten file de khong bi trung file + phan mo rong cua file can upload
                strFileName = Common.GetFileName() + System.IO.Path.GetExtension(FilePhoto.FileName);

                string strExtension = System.IO.Path.GetExtension(FilePhoto.FileName).ToUpper();
                if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" || strExtension ==".PNG")
                {
                    //duong dan tao file 
                    string strFilePath = Request.PhysicalApplicationPath + "//Gallery//" + txtAppKey.Text.Trim() + "//" + strFileName;

                    // neu PhotoSize = -1 se upload size goc
                    FileProcess.UpLoadImage(FilePhoto, strFilePath, (int)PhotoSize.Medium);
                }
                else
                {
                    strFileName ="";
                }
            }

            return strFileName;
        }


        // load PhotoCategory khi clicked button edit
        private void Load_AlbumByCateID(int CateID)
        {
            webapp4U.BOL.PhotoCategory obj = webapp4U.BOL.PhotoCategory.GetCateByCateID(CateID);
            txtAppKey.Text = obj.AppKey;
            txtAppKey.Enabled = false;
            txtAlbumName.Text = obj.CateName;
            txtAlbumName_en.Text = obj.CateName_en;
            txtAlbumDesc.Text = obj.CateDesc;
            txtCateOrder.Text = obj.CateOrder.ToString();
            chkIsPrivate.Checked = CheckAlbumPrivate(obj.CateID);

            //luu lai de kiem tra khi update
            ViewState["image"] = obj.ImageUrl;
        }

        // kiem tra xem co phai Album Private ko
        protected bool CheckAlbumPrivate(int CateID)
        {
            webapp4U.BOL.PhotoCategory obj = webapp4U.BOL.PhotoCategory.GetCateByCateID(CateID);
            if (obj.ParentCateID == (int)eCategory.AlbumsPrivate)
                return true;
            else
                return false;
        }


        protected void AlbumsAdd_Click(object sender, EventArgs e)
        {
            if (btAlbumAdd.CommandArgument == "Edit")
            {
                AlbumEdit();
                // khoi phuc lai trang thai Add cho button
                btAlbumAdd.CommandArgument = "Add";
            }
            else
                AlbumAdd();

            // xoa sach du lieu tren form
            this.Form_Clear();
        }
        

        private void AlbumAdd()
        {
            //// Kiem Tra va tao Folder cho album 
            //if (webapp4U.BOL.PhotoCategory.CheckAppKey(txtAppKey.Text.Trim()) || CreateFolderAlbum(txtAppKey.Text.Trim()) == false)
            //{
            //    lblMsg.Visible = true;
            //    lblMsg.Text = "Tên thư mục này đã có, bạn hãy chọn thư mục khác!";
            //}
            //else
            //{
                // thuc hien them du lieu vao database
                webapp4U.BOL.PhotoCategory obj = new webapp4U.BOL.PhotoCategory();
                obj.ImageUrl = FilePhoto_Upload(); // upload va return ten file
                obj.AppKey = txtAppKey.Text.Trim();
                obj.CateName = txtAlbumName.Text.Trim();
                obj.CateDesc = txtAlbumDesc.Text;
                obj.CateName_en = txtAlbumName_en.Text;
                obj.CateDesc_en = "";
                if (txtCateOrder.Text == "")
                    obj.CateOrder = 0;
                else
                    obj.CateOrder = Convert.ToInt32(txtCateOrder.Text);
                if (chkIsPrivate.Checked)
                    obj.ParentCateID = Convert.ToInt32(eCategory.AlbumsPrivate);
                else
                    obj.ParentCateID = Convert.ToInt32(eCategory.AlbumsPublic);

                // thuc hien lenh insert
                webapp4U.BOL.PhotoCategory.Insert(obj);
            //}
        }
        
        // khi chinh sua duoc thuc hien
        private void AlbumEdit()
        {
            string strFileName = "";
            // neu thay doi hinh dai dien thi phai upload lai hinh dai dien
            if (FilePhoto.FileName != null && FilePhoto.FileName != "")
            {
                strFileName = FilePhoto_Upload();
            }
            else
                strFileName = ViewState["image"].ToString(); // lay lai hinh dai dien cu

            // thuc hien them du lieu vao database
            webapp4U.BOL.PhotoCategory obj = new webapp4U.BOL.PhotoCategory();
            obj.CateID = CateID;
            obj.ImageUrl = strFileName;
            obj.AppKey = txtAppKey.Text.Trim();
            obj.CateName = txtAlbumName.Text.Trim();
            obj.CateDesc = txtAlbumDesc.Text;
            obj.CateName_en = txtAlbumName_en.Text; ;
            obj.CateDesc_en = "";
            if (txtCateOrder.Text == "")
                obj.CateOrder = 0;
            else
                obj.CateOrder = Convert.ToInt32(txtCateOrder.Text);
            if (chkIsPrivate.Checked)
                obj.ParentCateID = Convert.ToInt32(eCategory.AlbumsPrivate);
            else
                obj.ParentCateID = Convert.ToInt32(eCategory.AlbumsPublic);

            // thuc hien lenh Update
            webapp4U.BOL.PhotoCategory.Update(obj);
            txtAppKey.Enabled = true;

        }



        private void Form_Clear()
        {
            GridViewItem.DataBind();
            txtAppKey.Text = "" ;
            txtAlbumDesc.Text = "";
            txtAlbumName.Text = "";
            txtCateOrder.Text = "0";
            chkIsPrivate.Checked = false;
            lblMsg.Visible = false;
        }


        // tao thu muc cho album
        protected bool CreateFolderAlbum(string AppKey)
        {
            //kiem tra xem thu muc nay da ton tai hay chua
            if (System.IO.Directory.Exists(Request.PhysicalApplicationPath + "//Gallery//" + AppKey))
                return false;
            else
            {
                //Tao thu muc goc
                System.IO.Directory.CreateDirectory(Request.PhysicalApplicationPath + "//Gallery//" + AppKey);
                
                //thu muc chua hinh small
                System.IO.Directory.CreateDirectory(Request.PhysicalApplicationPath + "//Gallery//" + AppKey + "//Small");

                //thu muc chua hinh large
                System.IO.Directory.CreateDirectory(Request.PhysicalApplicationPath + "//Gallery//" + AppKey + "//Large");

                return true;
            }
        }

        
        protected void GridViewItem_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton btnDelete = e.Row.Cells[3].Controls[0] as ImageButton;
                btnDelete.OnClientClick = "if (confirm('Nếu bạn chọn OK thì tất cả hình ảnh nằm trong Album này sẽ bị xóa?') == false) return false;";
                btnDelete.ToolTip = "Delete this post";
            }
        }
       
    }
}